package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Position;
import cn.wolfcode.crm.service.IPositionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("position")
public class PositionController {
    @Autowired
    private IPositionService positionService;

    @RequiresPermissions("position:index")
    @PermissionName("岗位首页")
    @RequestMapping("index")
    public String index() {
        return "position/list";
    }





    @RequiresPermissions("position:save")
    @PermissionName("岗位保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Position position){
        JsonResult result = new JsonResult();
        try {
            positionService.insert(position);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存岗位失败");
        }
        return result;
    }

    @RequiresPermissions("position:update")
    @PermissionName("岗位编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Position position){
        JsonResult result = new JsonResult();
        try {
            positionService.updateByPrimaryKey(position);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新岗位失败");
        }
        return result;
    }


    @RequestMapping("selectAll")
    @ResponseBody
    public List<Position> selectAll(){
        return positionService.selectAll();
    }

}
